package com.singleton;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Locale;
import java.util.ResourceBundle;
import com.mchange.v2.c3p0.ComboPooledDataSource;

/**枚举可解决线程安全问题。
 * 也可以对其进行改造，增加懒加载的特性，类似于Holder的方式
 *
 */
public class SingleEnum2 {

    // 使用枚举充当holder
    private enum EnumHolder {
    	
    	DATASOURCE;
    	private ComboPooledDataSource cpds = null;
    	
	    private EnumHolder() {
	    	try {
	            ResourceBundle rs = ResourceBundle.getBundle("jdbc", new Locale("zh","CN"));
	            cpds = new ComboPooledDataSource();
	            cpds = new ComboPooledDataSource();
	            cpds.setDriverClass(rs.getString("driverClass"));
	            cpds.setJdbcUrl(rs.getString("jdbcUrl"));
	            cpds.setUser(rs.getString("user"));
	            cpds.setPassword(rs.getString("password"));
	            cpds.setMaxPoolSize(Integer.parseInt(rs.getString("maxPoolSize")));
	            cpds.setMinPoolSize(Integer.parseInt(rs.getString("minPoolSize")));
	            System.out.println("-----调用了构造方法------");
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	    
	    public Connection getConnection() {
	        try {
	            return cpds.getConnection();
	        } catch (SQLException e) {
	            return null;
	        }
	    }
    }
    
    public static Connection getConnection() {
    	return SingleEnum2.EnumHolder.DATASOURCE.getConnection();
    }

    public static void main(String[] args) {
    	SingleEnum2.getConnection();
	}

}
